package com.landmesure.bluetoothchat;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.code.EntityType;
import com.entrance.FragmentUpdate;
import com.landmesure.bluetoothchat.constant.Constant;
import com.landmesure.communication.BaseManager;
import com.landmesure.communication.SendMessageService;
import com.landmesure.communication.TaskOvertimeMonitor;

import java.util.ArrayList;

public class LandCommunicationService extends Service {
    private MyBinder myBinder = new MyBinder();
    private ArrayList<EntityType> entityTypeArrayList = new ArrayList<>();
    private String rcvMessage;//返回消息，用于显示到页面
    private BluetoothChatService bluetoothChatService;

    public BluetoothChatService getBluetoothChatService() {
        return bluetoothChatService;
    }

    public void setBluetoothChatService(BluetoothChatService bluetoothChatService) {
        this.bluetoothChatService = bluetoothChatService;
    }

    public String getRcvMessage() {
        return rcvMessage;
    }

    public void setRcvMessage(String rcvMessage) {
        this.rcvMessage = rcvMessage;
    }

    public class MyBinder extends Binder {

        public LandCommunicationService getInstance() {
            return LandCommunicationService.this;
        }
    }
    public void setBaseManager(BaseManager baseManager){
        while (bluetoothChatService==null){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        bluetoothChatService.setBaseManager(baseManager);
    }
    public void setData(ArrayList<EntityType> data) {
        LandCommunicationService.this.entityTypeArrayList = data;
    }

    public ArrayList<EntityType> getData() {
        return entityTypeArrayList;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public void onCreate() {
        TaskOvertimeMonitor taskOvertimeMonitor=new TaskOvertimeMonitor();
        taskOvertimeMonitor.start();
    }

    /**
     * 手机端设置参数handler
     */
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {


            switch (msg.what) {
                case Constant.SETUP_PARAMETERS: {
                    SendMessageService sendMessageService = new SendMessageService();
                    try {
                        sendMessageService.sendMessage(entityType,Constant.SETUP_PARAMETERS, code,null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
                case Constant.RESET: {
                    SendMessageService sendMessageService = new SendMessageService();
                    try {
                        sendMessageService.sendMessage((EntityType) msg.obj, Constant.RESET, null,null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
                }
                case Constant.SELECT_TEST: {
                    SendMessageService sendMessageService = new SendMessageService();
                    try {
                        sendMessageService.sendMessage((EntityType) msg.obj, Constant.SELECT_TEST, null,null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
                }
                default:
                    break;
            }
        }
    };

    public Handler getHandler() {
        return mHandler;
    }

    /**
     * 手机端设置参数，并在此取得;
     */
    private EntityType entityType;
    private ArrayList<String> code;

    public void setArgs(EntityType entityType, ArrayList<String> code) {
        this.entityType = entityType;
        this.code = code;
    }

    public EntityType getEntityType() {
        return entityType;
    }

    public ArrayList<String> getCode() {
        return code;
    }

    /**
     * UpdateFragment实例
     */
    private FragmentUpdate.UpdateFragment updateFragment;

    public void setUfInstance(FragmentUpdate.UpdateFragment fragment) {
        updateFragment = fragment;
    }

    public FragmentUpdate.UpdateFragment getUfInstance() {
        return updateFragment;
    }
}
